
package com.jojoteamwork.photo.ui;

import com.jojoteamwork.photo.R;
import com.jojoteamwork.photo.provider.Model;
import com.jojoteamwork.photo.util.ImageCache;
import com.jojoteamwork.photo.util.ImageCache.ImageCacheParams;
import com.jojoteamwork.photo.util.ImageWorker;
import com.jojoteamwork.photo.util.ImageZipLoader;
import com.jojoteamwork.photo.util.Utils;
import com.jojoteamwork.photo.util.ZipDataReader;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;

public class PhotoBrowseActivity extends FragmentActivity {

    private static final String IMAGE_CACHE_DIR = "images";

    public static final String EXTRA_IMAGE = "extra_image";

    private ViewPager pager;

    private Button btnPrev;

    private Button btnNext;

    private MoviePagerAdapter adapter;

    private ImageWorker mImageWorker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_main);

        Model model = (Model) getIntent().getSerializableExtra("model");
        String zipPath = Utils.MAGAZINE_DIR + "/" + getIntent().getStringExtra("filename");
        btnPrev = (Button) findViewById(R.id.btnPrev);
        btnNext = (Button) findViewById(R.id.btnNext);

        mImageWorker = new ImageZipLoader(this);
        ImageCacheParams cacheParams = new ImageCacheParams(IMAGE_CACHE_DIR);
        cacheParams.diskCacheEnabled = false;
        cacheParams.memCacheSize = 1024 * 1024 * Utils.getMemoryClass(this) / 3;
        mImageWorker.setImageCache(ImageCache.findOrCreateCache(this, cacheParams));
        mImageWorker.setImageFadeIn(true);

        ArrayList<String> photoPaths = ZipDataReader.getFileNamesInDirectory(zipPath, model.pic);
        adapter = new MoviePagerAdapter(getSupportFragmentManager(), zipPath, photoPaths);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
        // pager.setCurrentItem(0);
        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                setNextPrevVisibility(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        setNextPrevVisibility(0);
    }

    private void setNextPrevVisibility(int position) {
        if (position <= 0) {
            btnPrev.setVisibility(View.INVISIBLE);
        } else {
            btnPrev.setVisibility(View.VISIBLE);
        }
        if (position >= adapter.getCount() - 1) {
            btnNext.setVisibility(View.INVISIBLE);
        } else {
            btnNext.setVisibility(View.VISIBLE);
        }

    }

    public ImageWorker getImageWorker() {
        return mImageWorker;
    }

    public class MoviePagerAdapter extends FragmentStatePagerAdapter {
        private ArrayList<String> mPaths;

        private String mZipPath;

        public MoviePagerAdapter(FragmentManager fm, String zipPath, ArrayList<String> photoPaths) {
            super(fm);
            mPaths = photoPaths;
            mZipPath = zipPath;
        }

        @Override
        public Fragment getItem(int position) {
            return PhotoBrowseFragment.newIntance(mZipPath, mPaths.get(position));
        }

        @Override
        public int getCount() {
            return mPaths.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            final PhotoBrowseFragment fragment = (PhotoBrowseFragment) object;
            fragment.cancelWork();
            super.destroyItem(container, position, object);
        }
    }

}
